﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WebReference;



public partial class Admin_EditReference : System.Web.UI.Page
{
    Service objServices = new Service();


    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
        }
        else
        {
            bindDataGrid();
            trDgEditReference.Visible = true;
        }
    }
   
    private void fillReferenceDetails(Int32 Id)
    {

        DataSet ds = new DataSet();
        ds = objServices.GetReferenceDetails(Id);

        txtAuthor.Text = ds.Tables[0].Rows[0]["author"].ToString();
        txtCitation.Text = ds.Tables[0].Rows[0]["citation"].ToString();
        txtOraganization.Text = ds.Tables[0].Rows[0]["organizations"].ToString();
        txtReferenceID.Text = ds.Tables[0].Rows[0]["ref_id"].ToString();

    }

    private void bindDataGrid()
    {
        dgReference.DataSource = objServices.GetReference().Tables[0];
        dgReference.DataBind();
    }


    protected void dgReference_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = Convert.ToInt32(dgReference.Rows[e.NewEditIndex].Cells[1].Text.Trim());
        fillReferenceDetails(id);
        trEditReference.Visible = true;
        trDgEditReference.Visible = false;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (txtAuthor.Text == string.Empty || txtCitation.Text == string.Empty || txtOraganization.Text == string.Empty)
        {
            lblMessage.Text = "Please select a row of record form the Referecne Table";
        }
        else
        {
            if (txtReferenceID.Text != string.Empty)
            {
                lblMessage.Text = objServices.UpdateReference(txtAuthor.Text, txtCitation.Text, txtOraganization.Text, txtReferenceID.Text);
                bindDataGrid();
            }
            else
            {
                lblMessage.Text = "Select the Edit row from the data grid";
            }
        }
    }
    protected void btnGo_Click(object sender, EventArgs e)
    {
        if (txtReferenceID.Text == string.Empty)
        {
            lblMessage.Text = "Please select the reference to edit";
        }
        else
        {
            Session["ReferenceId"] = txtReferenceID.Text;
           // Response.Redirect("ChooseExperiment.aspx");
            Response.Redirect("SelectExperiment.aspx");
        }
    }

    protected void dgReference_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        dgReference.PageIndex = e.NewPageIndex;
        bindDataGrid();
    }
}
